package com.cpi.cornpp.futuresversions.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.futuresversions.exception.CornFuturesVersionsException;
import com.cpi.cornpp.futuresversions.services.CornFuturesVersionsService;
import com.cs.common.utils.NumberUtil;

/**
 * Corn Futures Versions Del Action
 * @author Edgar Joao
 *
 */
public class CornFuturesVersionsDelAction extends Action{	
	/** Identifier default workflow*/
	private final static String DEFAULT = "default";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$				
	/** Success add key */
	private static final String SUCCESS_DELELTE_LABEL = "common.delete.success";//$NON-NLS-1$	
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Error message name*/
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private final static String VERSION_ID = "versionId";
	/**
	 * Metodo execute
	 * @author Edgar Joao
	 */	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws CornFuturesVersionsException {
		MessageResources messageResources = null;
		String messageError = null;								
		HttpSession httpSession = null;					
		CornFuturesVersionsService cornFuturesVersionsService = null;
		int versionId = 0;
		int cornFutureVersionId = 0;
		try{			
			httpSession = request.getSession(false);
			messageResources = getResources(request);
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID));
			cornFutureVersionId = Integer.parseInt(request.getParameter("cornFuturesVersionsId"));
			cornFuturesVersionsService = new CornFuturesVersionsService();
			cornFuturesVersionsService.deleteCornFuturesVersions(versionId, cornFutureVersionId);
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_DELELTE_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (CornFuturesVersionsException cornFuturesVersionsException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), cornFuturesVersionsException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, cornFuturesVersionsException);
				throw cornFuturesVersionsException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
			}
		}catch (Exception e){
			CornFuturesVersionsException costElementException=null;
			e.printStackTrace(System.out);
			costElementException= new CornFuturesVersionsException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_INSERT);
			httpSession.setAttribute(CUSTOM_EXCEPTION, costElementException);
			throw costElementException;
		} 
		return mapping.findForward(DEFAULT);
	}
}
